package com.yn.xgame.game;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.Iterator;

/**
 * GameCenterManagerParser
 * Created by wangboo on 17/7/15.
 */
public class GameCenterManagerParser {

    public static GameCenterManager parse(String file) throws Exception {
        SAXReader reader = new SAXReader();
        InputStream is = GameCenterManagerParser.class.getClassLoader().getResourceAsStream(file);
        Document doc = reader.read(is);
        Element root = doc.getRootElement();
        int id = getInt(root, "id");
        GameCenterManager mgr = new GameCenterManager(id);
        Iterator<Element> it = root.elementIterator("center");
        while(it.hasNext()) {
            mgr.addCenterManager(parseCenter(it.next()));
        }
        return mgr;
    }

    private static CenterManager parseCenter(Element e) throws Exception {
        try {
            String name = e.attribute("name").getValue();
            CenterManager centerManager = new CenterManager(name);
            Iterator<Element> it = e.elementIterator("room");
            while (it.hasNext()) {
                centerManager.addRoomManager(parseRoom(it.next()));
            }
            return centerManager;
        } catch (Exception e1) {
            throw new RuntimeException("parse center " + e + " failed: ", e1);
        }

    }

    private static RoomManager parseRoom(Element e) throws Exception {
        //<room id="1" table="25" tableSize="8" goldPoint="-1" gameserver="1"/>
        try {
            int id = getInt(e, "id");
            String typeStr = e.attributeValue("type");
            if (!RoomManager.type_map.containsKey(typeStr)) {
                throw new RuntimeException("parse center.xml error, room.type not exists!");
            }
            int type = RoomManager.type_map.get(typeStr);
            int table = 1;
            if (type == RoomManager.TYPE_AUTO_MATCH) {
                table = getInt(e, "table");
            }
            int gameserverid = getInt(e, "gameserverid");
            int tableSize = getInt(e, "tableSize");
            int goldPoint = -1;
            if (e.attribute("goldPoint") != null) goldPoint = getInt(e, "goldPoint");
            int minStartSize = tableSize;
            if (e.attribute("minStartSize") != null) {
                minStartSize = getInt(e, "minStartSize");
            }
            // id, gameserverid, tableSize, goldPoint, minStartSize, type
            RoomManager roomMgr = null;
            if (type == RoomManager.TYPE_AUTO_MATCH) {
                roomMgr = new AutoMatchRoomManager(id, gameserverid, tableSize, goldPoint, minStartSize, type);
            }
            if (e.attribute("showName") != null) {
                String showName = e.attributeValue("showName");
                roomMgr.setShowName(showName);
            }
            return roomMgr;
        } catch (Exception e1) {
            throw new RuntimeException("parse room " + e + " failed: ", e1);
        }

    }

    private static int getInt(Element ele, String key) throws Exception {
        try {
            Attribute attr = ele.attribute(key);
            if (attr == null) {
                throw new RuntimeException("key: " + key + " not exists!!");
            }
            return Integer.parseInt(attr.getValue());
        }catch (Exception e) {
            throw new RuntimeException("parse ");
        }
    }

}
